package com.future;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 448. 找到所有数组中消失的数字 （渣打银行面试题）
 *
 * @author weiruibai.vendor
 * Date: 2022/7/25 22:41
 */
public class Solution_448 {

    public static void main(String[] args) {
        Solution_448 solution_448 = new Solution_448();
        int[] nums = new int[]{4, 3, 2, 7, 8, 2, 3, 1};
        nums = new int[]{1, 1};
        List<Integer> disappearedNumbers = solution_448.findDisappearedNumbers(nums);
        System.out.println(disappearedNumbers);
    }

    public List<Integer> findDisappearedNumbers(int[] nums) {
        int N = nums.length;
        BitMap bitMap = new BitMap(N);
        for (int i = 0; i < N; i++) {
            bitMap.add(nums[i]);
        }
        List<Integer> ans = new ArrayList<>();
        /**
         *  取值到N
         */
        for (int i = 1; i <= N; i++) {
            if (!bitMap.isContains(i)) {
                ans.add(i);
            }
        }
        return ans;
    }


    class BitMap {
        long[] arr;

        public BitMap(int max) {
            this.arr = new long[(max + 64) >> 6];
        }

        public void add(int num) {
            int index = num >> 6;
            arr[index] = arr[index] | (1L << (num % 64));
        }

        public boolean isContains(int num) {
            int index = num >> 6;
            return (arr[index] & (1L << (num % 64))) != 0;
        }
    }
}

